import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

// 导入内置的 logger 插件
import createLogger from 'vuex/dist/logger'

import moduleA from './a.module'

const store = new Vuex.Store({
    state: { // data
        counter: 10,
        msg: 'Hello Vuex',
        isAdmin: false,
        list: [
            { id: 1, text: '吃饭' },
            { id: 2, text: '睡觉' },
            { id: 3, text: '打豆豆' },
            { id: 4, text: '吃午饭' },
            { id: 5, text: '睡午觉' },
        ]
    },
    getters: { // computed
        counter100(state) {
            return (state.counter * 100).toFixed(5)
        },
        filterList: state => state.list.filter(item => item.text.includes('睡'))
    },
    mutations: { // 只是用来修改 state，不能执行异步操作
        SET_LIST(state, payload) {
            state.list.push({id: Date.now(), text: payload})
        },
        UPDATE_COUNTER(state) {
            state.counter++
        },
        SET_MSG(state) {
            state.msg = state.msg.split('').reverse().join()
        }
    },
    actions: {
        // 可以提交 mutations，执行异步操作
        setCounterAction(context) {
            context.commit('UPDATE_COUNTER')
        },
        setMsgAction({ commit }) {
            setTimeout(() => {
                commit('SET_MSG')
            }, 3000)

            /*
            axios({
                url: '/abc'
            }).then(res => {
                commit('SET_LIST', res.data)
            })*/
        }
    },
    modules: {
        a: moduleA
    },
    plugins: [createLogger()],
    strict: true, // 开启 vuex 严格模式
})
export default store

// console.log(store.state.counter)
// store.commit('updateCounter') // 修改 counter
// console.log(store.state.counter)

